﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace super_scrable.Views
{
    public partial class Properties : Form
    {
        public Properties()
        {
            InitializeComponent();
            string app_data_dir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);

            //init lisbox active language
            StringCollection active_language = super_scrable.Properties.Settings.Default.active_languages;
            foreach (string language in active_language)
            {
                this.listBoxLangueActive.Items.Add(language);
            }

            //init listbox available language 
            string[] language_available = Directory.GetFiles(Path.Combine(app_data_dir , super_scrable.Properties.Settings.Default.language_dir),"*.txt");
            foreach (string language in language_available)
            {
                if(isInActiveLanguage(Path.GetFileNameWithoutExtension(language)))
                    continue;

                this.listLangueDispo.Items.Add(Path.GetFileNameWithoutExtension(language));
            }
        }

        private bool isInActiveLanguage(string language)
        {
            foreach (string item in this.listBoxLangueActive.Items) {
                if (item == language)
                    return true;
            }

            return false;
        }

        private void button_activate_Click(object sender, EventArgs e)
        {
            int index = this.listLangueDispo.SelectedIndex;
            if (index >= 0)
            {
                this.listBoxLangueActive.Items.Add(this.listLangueDispo.Items[index]);
                this.listLangueDispo.Items.RemoveAt(index);
            }
        }

        private void button_desactivate_Click(object sender, EventArgs e)
        {
            int index = this.listBoxLangueActive.SelectedIndex;
            if (index >= 0)
            {
                this.listLangueDispo.Items.Add(this.listBoxLangueActive.Items[index]);
                this.listBoxLangueActive.Items.RemoveAt(index);
            }
        }

        private void button_annuler_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button_valider_Click(object sender, EventArgs e)
        {
            if(this.listBoxLangueActive.Items.Count < 1 ){
                MessageBox.Show("Veuillez choisir au moins un package de langue !!");
                return;
            }

            StringCollection active_language = new StringCollection();
            for (int i = 0; i < this.listBoxLangueActive.Items.Count; i++)
            {
                active_language.Add(this.listBoxLangueActive.Items[i].ToString());
            }
            super_scrable.Properties.Settings.Default.active_languages = active_language;
            super_scrable.Properties.Settings.Default.Save();
            this.Close();
        }

        private void button_repertoire_langue_Click(object sender, EventArgs e)
        {
            string app_data_dir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
            string language_dir = Path.Combine(app_data_dir , super_scrable.Properties.Settings.Default.language_dir);
            MessageBox.Show("Informations:\nSont pris en compte que les fichiers .txt\nChaques mots doit être séparé par un saut de ligne");
            System.Diagnostics.Process.Start(language_dir);//ouverture explorer dans le fichier de langue
            this.Close();
        }
    }
}
